package com.starleague.battletech.android;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

import com.google.common.eventbus.EventBus;
import com.starleague.android.event.HasEventBus;
import com.starleague.battletech.hexagonal.BattletechHexGridGenerator;
import com.starleague.battletech.hexagonal.IBattletechHex;
import com.starleague.battletech.hexagonal.IBattletechHexGrid;
import com.starleague.battletech.hexagonal.android.AndroidBattletechHex;
import com.starleague.battletech.hexagonal.android.AndroidBattletechHexGrid;
import com.starleague.battletech.hexagonal.android.AndroidBattletechHexGridFactory;
import com.starleague.battletech.hexagonal.android.AndroidBattletechHexTerrain;
import com.starleague.hexagonal.HexException;
import com.starleague.hexagonal.HexOutOfBoundsException;
import com.starleague.hexagonal.IHexCoordinate;
import com.starleague.hexagonal.android.HexDetailFragment;
import com.starleague.hexagonal.android.event.HexListEvent;
import com.starleague.hexagonal.android.event.HexSelectedEvent;
import com.starleague.hexagonal.flat.FlatHexGridUtil;
import com.starleague.hexagonal.flat.HasFlatHexGrid;

public class BattletechActivity
        extends Activity
        implements HasEventBus, HasFlatHexGrid
{
    private static final Logger log = LoggerFactory.getLogger(BattletechActivity.class);

    private final EventBus eventBus = new EventBus();
    private boolean isDualPane;

    private IBattletechHexGrid battletechHexGrid;

    private final FlatHexGridUtil flatHexGridUtil = new FlatHexGridUtil();
    private IBattletechHex selectedHex;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        int numHexesX = 32;
        int numHexesY = 32;

        BattletechHexGridGenerator<AndroidBattletechHexGrid, AndroidBattletechHex, AndroidBattletechHexTerrain> hexGridGenerator = new BattletechHexGridGenerator<AndroidBattletechHexGrid, AndroidBattletechHex, AndroidBattletechHexTerrain>(new AndroidBattletechHexGridFactory());

        try
        {
            this.battletechHexGrid = hexGridGenerator.generateRandomHexGrid(numHexesX, numHexesY);
        }
        catch (HexException e)
        {
            throw new RuntimeException(e);
        }

        setContentView(R.layout.activity_battletech);

        this.isDualPane = (getFragmentManager().findFragmentById(R.id.hex_details_fragment) != null);

        // this.eventBus.register(this);
    }

    @Override
    public EventBus getEventBus()
    {
        return this.eventBus;
    }

    @Override
    public IBattletechHexGrid getHexGrid()
    {
        return this.battletechHexGrid;
    }

    // public IBattletechHex getSelectedHex()
    // {
    // return this.selectedHex;
    // }

    //
    // Handle Events
    //

    public void selectHexCoordinate(IHexCoordinate hexCoordinate)
    {
        log.error("Activity.selectHexCoordinate: {}", hexCoordinate);

        try
        {
            this.selectedHex = this.battletechHexGrid.getHexAt(hexCoordinate);
        }
        catch (HexOutOfBoundsException e)
        {
            log.info("Activity.selectHexCoordinate: " + e);
            this.selectedHex = null;
        }

        this.eventBus.post(new HexSelectedEvent(this.selectedHex));

        if (!this.isDualPane)
        {
            HexDetailFragment hexDetailFragment = new HexDetailFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();

            ft.replace(R.id.fragment_container, hexDetailFragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

            ft.addToBackStack(null);
            ft.commit();
        }
    }

    public void setDestinationHexCoordinate(IHexCoordinate hexCoordinate)
    {
        log.info("Activity.setDestinationHexCoordinate: {}", hexCoordinate);

        if (this.selectedHex != null)
        {
            try
            {
                IBattletechHex destinationHex = this.battletechHexGrid.getHexAt(hexCoordinate);

                List<IHexCoordinate> hexCoordinatePath = this.flatHexGridUtil
                        .calculateLineOfSightPath(this.selectedHex.getHexCoordinate(),
                                                  destinationHex.getHexCoordinate());

                List<IBattletechHex> battletechHexList = new ArrayList<IBattletechHex>(hexCoordinatePath
                        .size());
                for (IHexCoordinate pathHexCoordinate : hexCoordinatePath)
                {
                    battletechHexList.add(this.battletechHexGrid.getHexAt(pathHexCoordinate));
                }

                this.eventBus.post(new HexListEvent(battletechHexList));
            }
            catch (HexOutOfBoundsException e)
            {
                log.error("Activity.setDragHexCoordinate Failed: ", e);
            }
        }
    }
}
